package com.library.controller;

import com.alibaba.fastjson.JSON;
import com.library.dao.TypeInfoDao;
import com.library.entity.TypeInfo;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/type/findAllList")
public class TypeInfoServlet extends HttpServlet {
    private TypeInfoDao typeInfoDao = new TypeInfoDao();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("this is findAllList(type)");
        List<TypeInfo> typeList = typeInfoDao.findAllTypes();
        for (int i = 0; i < typeList.size(); i++) {
            System.out.println(typeList.get(i));
        }
        Map<String, Object> result = new HashMap<>();
        result.put("code", 0);
        result.put("data", typeList);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(JSON.toJSONString(result));
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}